package com.effects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

/**
 * @author sad
 */
public class DrawLinesEffect extends BasicEffect {

    private class MyPoint {

        private double x;
        private double y;
        private double dx;
        private double dy;
        private final Color color;

        public MyPoint() {
            x = Math.random() * 70 + 1;
            y = Math.random() * 30 + 1;
            dx = Math.random() * 3 + 0.1;
            dy = Math.random() * 3 + 0.1;
            color = getRainbow((int) (Math.random() * 70), 0);
        }
    }

    private final MyPoint[] points = new MyPoint[10];

    public DrawLinesEffect() {
        for (int i = 0; i < points.length; i++) {
            points[i] = new MyPoint();
        }
    }

    @Override
    public String getEffectName() {
        return "DrawLines";
    }

    @Override
    public String getId() {
        return "drawLines";
    }

    @Override
    public BufferedImage getImage(byte[] pcm) {
        alphaClearingImage(image, 5);
        Graphics g = image.getGraphics();
        for (MyPoint p : points) {
            p.x += p.dx;
            p.y += p.dy;
            if (p.x < 0) {
                p.dx *= -1;
            }
            if (p.x > 78) {
                p.dx *= -1;
            }
            if (p.y < 0) {
                p.dy *= -1;
            }
            if (p.y > 39) {
                p.dy *= -1;
            }
            g.setColor(p.color);
            g.fillOval((int) p.x - 2, (int) p.y - 2, 4, 4);
        }
        return image;
    }

}
